package com.colin.app.activity.view.edittext;

import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.activity.view.ViewContract;
import com.colin.app.activity.view.ViewPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.library.listener.MyTextWatcher;
import com.colin.library.view.FormatEditText;

public class EditTextActivity extends AppActivity implements ViewContract.ViewView {
    private ViewContract.ViewPresenter mViewPresenter = null;
    private EditText edit_password;
    private ImageView image_delete_password;
    private ImageView image_password_hide;


    private FormatEditText clearXEdit;
    private FormatEditText customXEdit;
    private FormatEditText showXEdit;
    private TextView textView1, textView2;
    private Button button;

    @Override
    protected void onDestroy() {
        mViewPresenter.destroy();
        mViewPresenter = null;
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mViewPresenter = new ViewPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_edit_text;
    }

    @Override
    public void initView() {
        this.edit_password = (EditText) this.findViewById(R.id.edit_password);
        this.image_delete_password = (ImageView) this.findViewById(R.id.image_delete_password);
        this.image_password_hide = (ImageView) this.findViewById(R.id.image_password_hide);
        this.edit_password = (EditText) this.findViewById(R.id.edit_password);

        clearXEdit = (FormatEditText) findViewById(R.id.clear_marker_edit_text);
        customXEdit = (FormatEditText) findViewById(R.id.custom_edit_text);
        showXEdit = (FormatEditText) findViewById(R.id.show_separator_edit_text);
        textView1 = (TextView) findViewById(R.id.text1);
        textView2 = (TextView) findViewById(R.id.text2);
        button = (Button) findViewById(R.id.show_pattern_btn);

        customXEdit.setPattern(new int[]{4, 4, 4, 4});
    }

    @Override
    public void initData() {
        mViewPresenter.setData();
    }

    @Override
    public void initListener() {
        this.edit_password.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                mViewPresenter.editTextChanged(s.toString(), image_delete_password);
            }
        });

        this.image_password_hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPresenter.editPasswordShow(image_password_hide, edit_password);
            }
        });

        this.image_delete_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPresenter.editDeleteText(edit_password);
            }
        });


        clearXEdit.setOnFormatTextChangeListener(new FormatEditText.OnFormatTextChangeListener() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                textView1.setText(clearXEdit.getTrimmedString());
            }
        });
        customXEdit.setOnFormatTextChangeListener(new FormatEditText.OnFormatTextChangeListener() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                textView2.setText(customXEdit.getTrimmedString());
            }
        });

        showXEdit.setSeparator(" ");
        showXEdit.setPattern(new int[]{3, 4, 4});
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showXEdit.setTextToSeparate("13800000000");
                }
            });
        }

    }
}
